package baiduClub.schedule;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
	
	public static final String flag = "baiduClub.schedule.MainActivity";
	
	XmlConductor xmlReader = new XmlConductor();
	private ImageView exitButton = null;
	private LinearLayout dateButton = null;
	private View backButton = null;
	private View frontButton = null;
	private ImageView todayButton = null;
	private String xmlDir = null;
	private TextView day_of_week;
	private TextView yy_MM_dd;
	private TextView week_TextView;
	private MyAdapter listAdapter;

	
	public static int yearOfSet,monthOfSet,dayOfSet;
	
    CalculateDate calculateDate = new CalculateDate();
    File file = null;
    File subfile = null;
    
    
    Calendar dateCalendar = null;
    Calendar dateCalendar2 = null;
    int yearOfNow;
    int monthOfNow;
    int dayOfNow;
    int dayOfWeekOfNow;
    int dayOfMonthOfNow;
    int weekOfNow;
    
    int dayOfYearOfNow;  
    int dayOfYearOfFirstDay; 
      
	int year;
	int month;
	int day;
	int dayOfWeek;
	int week;
    
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    
    
    HashMap<String, String> map1 = new HashMap<String, String>();
    HashMap<String, String> map2 = new HashMap<String, String>();
    HashMap<String, String> map3 = new HashMap<String, String>();
    HashMap<String, String> map4 = new HashMap<String, String>();
    HashMap<String, String> map5 = new HashMap<String, String>();
    HashMap<String, String> map6 = new HashMap<String, String>();
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        setResult(RESULT_OK);
        
        dateCalendar = Calendar.getInstance();
        dateCalendar2 = Calendar.getInstance();
        
        yearOfNow = yearOfSet = dateCalendar.get(Calendar.YEAR);
        monthOfNow  = monthOfSet = dateCalendar.get(Calendar.MONTH);
        dayOfNow = dayOfSet = dateCalendar.get(Calendar.DAY_OF_MONTH);
        dayOfWeekOfNow = dateCalendar.get(Calendar.DAY_OF_WEEK);
        dayOfMonthOfNow = dateCalendar.get(Calendar.DAY_OF_MONTH);
        
        SharedPreferences settings = getSharedPreferences("baiduClub.schedule", 0);
        xmlDir = settings.getString("xmlDir", null);
        
        file = new File(xmlDir);
        subfile = new File("/mnt//sdcard//fubiao.xml"); 
        
        //若附表不存在时
        if( !subfile.isFile() )
        {
        	try {
				subfile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        dateCalendar2.set(2012, 8, 4);
        weekOfNow = calculateDate.calculateWeek(dateCalendar2, dateCalendar);
        System.out.println(dateCalendar.get(Calendar.DAY_OF_YEAR));
        System.out.println(dateCalendar2.get(Calendar.DAY_OF_YEAR));
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        list.add(map6);
        
        putMaps(weekOfNow, dayOfWeekOfNow, file, subfile);
        
        listAdapter = new MyAdapter(this, list,
				R.layout.user, new String[] { "class", "time", "courseName", "place0","place1"},
				new int[] { R.id.class_turn,R.id.time_label,R.id.class_name,R.id.place,R.id.room});
		setListAdapter(listAdapter);
		
		day_of_week = (TextView)findViewById(R.id.day_of_week_tv);
		yy_MM_dd = (TextView)findViewById(R.id.yy_MM_dd);
		week_TextView = (TextView)findViewById(R.id.week);
		
        
        exitButton = (ImageView)findViewById(R.id.exit_btn);
        exitButton.setOnClickListener(new ExitButtonListener());
        
        dateButton = (LinearLayout)findViewById(R.id.date_chooser);
        
        System.out.println( yearOfNow + "年" + (monthOfNow + 1) + "月" + dayOfNow + "日");
        
        day_of_week.setText(calculateDate.calculateDayOfWeek(dayOfWeekOfNow));
        yy_MM_dd.setText(yearOfNow+"年"+(monthOfNow+1)+"月"+dayOfNow+"日");
        week_TextView.setText("第"+weekOfNow+"周");
        
        dateButton.setOnClickListener(new DateButtonListener());
        
        backButton = (View)findViewById(R.id.pre_day_btn);
        backButton.setOnClickListener(new BackButtonListener());
        
        frontButton = (View)findViewById(R.id.nxt_day_btn);
        frontButton.setOnClickListener(new FrontDateListener());
        
        todayButton = (ImageView)findViewById(R.id.today_btn);
        todayButton.setOnClickListener(new TodayButtonListener());
        
    }
    
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		dateCalendar.set(yearOfSet, monthOfSet, dayOfSet);
		
		week = calculateDate.calculateWeek(dateCalendar2, dateCalendar);
			
		dayOfWeek = dateCalendar.get(Calendar.DAY_OF_WEEK);
		//list.removeAll(list);
		putMaps(week, dayOfWeek, file, subfile);
//		dateButton.setText(dateCalendar.get(Calendar.YEAR)+"年"+(1+dateCalendar.get(Calendar.MONTH))+
//		    "月"+dateCalendar.get(Calendar.DAY_OF_MONTH)+"日"+" "+calculateDate.calculateDayOfWeek(dayOfWeek)
//		    +"     第"+week+"周");
		
		day_of_week.setText(calculateDate.calculateDayOfWeek(dayOfWeek));
		
		yy_MM_dd.setText(dateCalendar.get(Calendar.YEAR)+"年"+(1+dateCalendar.get(Calendar.MONTH))
				+"月"+dateCalendar.get(Calendar.DAY_OF_MONTH)+"日");
		week_TextView.setText("第"+week+"周");
		
		
		listAdapter.notifyDataSetChanged();
		/*MyAdapter listAdapter = new MyAdapter(MainActivity.this, list,
				R.layout.user, new String[] { "class", "time", "courseName", "place0","place1"},
				new int[] { R.id.class_turn,R.id.time_label,R.id.class_name,R.id.place,R.id.room});
		setListAdapter(listAdapter);*/
	}

    protected void onListItemClick(ListView l, View v, int position, long id)
    {
    	// TODO Auto-generated method stub
    	super.onListItemClick(l, v, position, id);
    		
    	Intent intent = new Intent();
    	intent.putExtra("turn", position);
    	intent.putExtra("week", week);
    	intent.putExtra("dayOfWeek", dayOfWeek);
    	intent.putExtra("courseName", xmlReader.courseNameString[position]);
    	intent.putExtra("place", xmlReader.placeString[position]);
    	intent.setClass(MainActivity.this, EditActivity.class);
    	startActivity(intent);
    }
    
     
	class ExitButtonListener implements OnClickListener 
    {
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			finish();
		}

		
    }
    
    class DateButtonListener implements OnClickListener
    {

		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			Intent intentToCheck = new Intent();
			intentToCheck.setClass(MainActivity.this, CheckActivity.class);
			startActivityForResult(intentToCheck, 0);
		}
    	
    }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode ==0&& resultCode == RESULT_OK) 
		{
			finish();
		}
	}
	
	
	public void putMaps(int week, int dayOfWeek,File file,File subfile)
    {
    	try 
    	{
    		xmlReader.readXml(week, dayOfWeek, file, subfile);
    	} catch (Exception e) 
    	{
    		// TODO Auto-generated catch block
    	}
    	
    	map1.clear();
    	map2.clear();
    	map3.clear();
    	map4.clear();
    	map5.clear();
    	map6.clear();
    	    	
    	map1.put("class", getString(R.string.classOne));
        map1.put("time", getString(R.string.timeOne));
        map2.put("class", getString(R.string.classTwo));
        map2.put("time", getString(R.string.timeTwo));
        map3.put("class", getString(R.string.classThree));
        map3.put("time", getString(R.string.timeThree));
        map4.put("class", getString(R.string.classFour));
        map4.put("time", getString(R.string.timeFour));
        map5.put("class", getString(R.string.classFive));
        map5.put("time", getString(R.string.timeFive));
        map6.put("class", getString(R.string.classSix));
        map6.put("time", getString(R.string.timeSix));
        
        for (int i = 0; i < xmlReader.placeString.length; i++) 
        {
			if (xmlReader.placeString[i] == null) {
				xmlReader.placeString[i] =  "";
			}
		}
    	
    	map1.put("courseName", xmlReader.courseNameString[0]);
    	String[] place_s = new String[2];
    	place_s = xmlReader.placeString[0].split(",");
    	for (int i = 0; i < place_s.length; i++) 
    	{	
    		if (place_s[i] == null) {
				place_s[i] = new String("");
			}
    		map1.put("place"+i, place_s[i]);
		}
        
        
        map2.put("courseName", xmlReader.courseNameString[1]);
        place_s = xmlReader.placeString[1].split(",");
    	for (int i = 0; i < place_s.length; i++) {
    		if (place_s[i] == null) {
				place_s[i] = new String("");
			}
    		map2.put("place"+i, place_s[i]);
		}
        
        map3.put("courseName", xmlReader.courseNameString[2]);
        place_s = xmlReader.placeString[2].split(",");
    	for (int i = 0; i < place_s.length; i++) {
    		if (place_s[i] == null) {
				place_s[i] = new String("");
			}
    		map3.put("place"+i, place_s[i]);
		}
        
        map4.put("courseName", xmlReader.courseNameString[3]);
        place_s = xmlReader.placeString[3].split(",");
    	for (int i = 0; i < place_s.length; i++) {
    		if (place_s[i] == null) {
				place_s[i] = new String("");
			}
    		map4.put("place"+i, place_s[i]);
		}
        
        map5.put("courseName", xmlReader.courseNameString[4]);
        place_s = xmlReader.placeString[4].split(",");
    	for (int i = 0; i < place_s.length; i++) {
    		if (place_s[i] == null) {
				place_s[i] = new String("");
			}
    		map5.put("place"+i, place_s[i]);
		}
        
        map6.put("courseName", xmlReader.courseNameString[5]);
        place_s = xmlReader.placeString[5].split(",");
    	for (int i = 0; i < place_s.length; i++) {
    		if (place_s[i] == null) {
				place_s[i] = new String("");
			}
    		map6.put("place"+i, place_s[i]);
		}
    }
    
	
    class BackButtonListener implements OnClickListener
    {
    	
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			
			dateCalendar.add(Calendar.DAY_OF_YEAR, -1);
			
			week = calculateDate.calculateWeek(dateCalendar2, dateCalendar);
				
			dayOfWeek = dateCalendar.get(Calendar.DAY_OF_WEEK);
			//list.removeAll(list);

			yy_MM_dd.setText(dateCalendar.get(Calendar.YEAR)+"年"+(1+dateCalendar.get(Calendar.MONTH))
					+"月"+dateCalendar.get(Calendar.DAY_OF_MONTH)+"日");
			week_TextView.setText("第"+week+"周");
			day_of_week.setText(calculateDate.calculateDayOfWeek(dayOfWeek));
			putMaps(week, dayOfWeek, file, subfile);
			
			listAdapter.notifyDataSetChanged();
			/*MyAdapter listAdapter = new MyAdapter(MainActivity.this, list,
					R.layout.user, new String[] { "class", "time", "courseName", "place0","place1"},
					new int[] { R.id.class_turn,R.id.time_label,R.id.class_name,R.id.place,R.id.room});
			setListAdapter(listAdapter);*/
		}
    }
    
    class FrontDateListener implements OnClickListener
    {
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
            dateCalendar.add(Calendar.DAY_OF_YEAR, 1);
            week = calculateDate.calculateWeek(dateCalendar2, dateCalendar);
			dayOfWeek = dateCalendar.get(Calendar.DAY_OF_WEEK);
			
			//list.removeAll(list);
			
			day_of_week.setText(calculateDate.calculateDayOfWeek(dayOfWeek));
			week_TextView.setText("第"+week+"周");
			yy_MM_dd.setText(dateCalendar.get(Calendar.YEAR)+"年"+(1+dateCalendar.get(Calendar.MONTH))
					+"月"+dateCalendar.get(Calendar.DAY_OF_MONTH)+"日");
			putMaps(week, dayOfWeek, file, subfile);
			listAdapter.notifyDataSetChanged();
			/*MyAdapter listAdapter = new MyAdapter(MainActivity.this, list,
					R.layout.user, new String[] { "class", "time", "courseName", "place0","place1"},
					new int[] { R.id.class_turn,R.id.time_label,R.id.class_name,R.id.place,R.id.room});
			setListAdapter(listAdapter);*/
		}
    }
    
    class TodayButtonListener implements OnClickListener
    {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			//list.removeAll(list);
			week = weekOfNow;
			dayOfWeek = dayOfWeekOfNow;
			putMaps(weekOfNow, dayOfWeekOfNow, file,subfile);
			
//			dateButton.setText(yearOfNow+"年"+(1+monthOfNow)+
//				    "月"+dayOfNow+"日"+" "+calculateDate.calculateDayOfWeek(dayOfWeekOfNow)
//				    +"     第"+weekOfNow+"周");
			
			day_of_week.setText(calculateDate.calculateDayOfWeek(dayOfWeekOfNow));
			yy_MM_dd.setText(yearOfNow+"年"+(1+monthOfNow)+"月"+dayOfNow+"日");
			week_TextView.setText("第"+weekOfNow+"周");
			
			
			listAdapter.notifyDataSetChanged();
			/*MyAdapter listAdapter = new MyAdapter(MainActivity.this, list,
					R.layout.user, new String[] { "class", "time", "courseName", "place0","place1"},
					new int[] { R.id.class_turn,R.id.time_label,R.id.class_name,R.id.place,R.id.room});
			setListAdapter(listAdapter);*/
		}
    	
    }
    
    public String getSDPath()
	{
    	File sdDir = null;
    	boolean sdCardExist = Environment.getExternalStorageState()
    	.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
    	if (sdCardExist)
    	{
    	sdDir = Environment.getExternalStorageDirectory();//获取跟目录
    	}
    	return sdDir.toString();
    }
}